<?php
/**
 * Created by PhpStorm.
 * User: 王松
 * Time: 2017/7/5 17:51
 */

namespace App\Project\Printer;

use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

abstract class PrinterAbstract
{
    /**
     * @var \Illuminate\Database\Eloquent\Model
     */
    protected $model;

    public function printer()
    {
        $data = [];

        foreach ($this->model->getOriginal() as $key => $item) {
            if (is_null($item) || '' === $item) {
                continue;
            }

            if (Str::contains($key, '_')) {
                $studly = lcfirst(Str::studly($key));
            } else {
                $studly = $key;
            }

            if (method_exists($this, $studly)) {

                $str = $this->$studly();

                if (!(is_null($str) || '' === $str)) {
                    $data[$key] = $str;
                }
            }
        }

        return $data;
    }

    public function getPrintStr($left, $print, $right = '；', $glue = '，')
    {
        if (is_array($print)) {

            foreach ($print as $key => &$item) {
                if (is_null($item) || '' === $item) {
                    unset($print[$key]);
                }
            }
            if (!empty($print)) {

                $print = implode($glue, $print);
            } else {
                return null;
            }
        }

        return $left . $print . $right."\n";
    }

    public function diffWithFormat($days, $end = 'now')
    {
        if ($days instanceof Carbon) {
            $days = $days->diffInDays();
        } elseif (!is_numeric($days)) {
            $days = (new Carbon($days))->diffInDays();
        }

        $time = '';

        if ($year = (int)($days / 365)) {
            $time = $year . '年';
        }

        if ($month = (int)(($days - $year * 365) / 30)) {
            $time .= $month . '个月';
        }

        if (($time === '') && ($day = ($days - $year * 365) - $month * 30)) {
            $time .= $day . '天';
        }

        return $time;
    }

    public function fed($field, $key = null, $left = '', $right = '')
    {
        return $this->fe($field, $key, $left, $right, function ($value) {

            return $this->diffWithFormat($value);
        });
    }

    public function fei($field, $key = null, $left = '', $right = '', $glue = '、')
    {
        return $this->fe($field, $key, $left, $right, function ($value) use ($glue) {
            return implode($glue, $value);
        });
    }

    public function fe($field, $key = null, $left = '', $right = '', \Closure $closure = null)
    {
        $value = $this->model->$field;

        if ('' !== $left) {
            $left = $left . ' ';
        }

        if ('' !== $right) {
            $right = ' ' . $right ;
        }

        if (is_null($value) || '' === $value) {
            return '';
        }

        if ((is_string($value) || is_numeric($value)) && '' !== $value) {

            if (!is_null($closure)) {
                $value = $closure($value);
            }

            return $left . $value . $right;
        }

        if (Arr::has($value, $key)) {

            $value = Arr::get($value, $key);

            if (is_null($value) || '' === $value) {
                return '';
            }

            if (!is_null($closure)) {
                $value = $closure($value);
            }

            return $left . $value . $right;
        }

        return '';
    }

    public function ft($file, $value, $left = '', $right = '')
    {
        if ('' === $value) {
            return '';
        }

        $file = app('field')->get($file);

        if (array_key_exists($value, $file)) {
            if (('' === $left) && ('' === $right)) {
                return $file[$value];
            }

            if ('' !== $left) {
                $left = $left . ' ';
            }

            return $left . $file[$value] . $right;
        }

        return '';
    }

    /**
     * 判断是否空数组,防止空字符串导致打印出问题
     */
    public function isEmpty($arr){
        if(is_array($arr)){
            $b=true;
            foreach ($arr as $k=>$v){
                if($v&&!is_array($v)&&!trim($v)==''){
                    $b=false;
                    break;
                }
                $t=$this->isEmpty($v);
                if(!$t){
                   return false;
                }

            }
            return $b;
        }else{
            if(!$arr){
                return true;
            }else
                return false;
        }
    }

    public function has($value, $key)
    {
        return isset($value[$key]) && !($value[$key] === '');
    }
}